//
//  MsgDescViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/11/6.
//  Copyright © 2018 wjk. All rights reserved.
//

import UIKit
import SwiftyJSON
import Alamofire
import Moya


class MsgDescViewController: UIViewController, UIDocumentInteractionControllerDelegate {
    @IBOutlet weak var labelTitle: UILabel!
    
    @IBOutlet weak var labelContent: UILabel!
    @IBOutlet weak var labelDate: UILabel!
    
    @IBOutlet weak var btnFile: UIButton!
    
    var bulletin_id:String!
    
    var j:JSON?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "公告详情"
        NetWorkRequest(.bulletin_id(id:bulletin_id)) { (res) -> (Void) in
            if res["code"] as! String == "000000" {
                self.j = jsonFormater(data: res["data" ] as Any)
                self.labelTitle.text = self.j?["title"].stringValue
                self.labelDate.text = self.j?["created_at"].stringValue
                self.labelContent.attributedText = try! NSAttributedString.init(data: (self.j?["content"].stringValue.data(using: String.Encoding.unicode))!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)

//                self.labelContent.text = self.j?["content"].stringValue
                if self.j?["attachment"].dictionaryValue["download_url"]?.stringValue.isEmpty == false {
                    self.btnFile.isHidden = false
                    
                    self.btnFile.accessibilityIdentifier = self.j?["attachment"].dictionaryValue["download_url"]?.stringValue
                    
                    self.btnFile.setTitle(self.j?["attachment"].dictionaryValue["name"]?.stringValue, for: .normal)
                }else {
                    self.btnFile.isHidden = true
                }
            }
        }
        // Do any additional setup after loading the view.
    }

    @IBAction func download(_ sender: UIButton) {
       
        let us = MoyaProvider<UserService>()
        
        us.request(UserService.download(url: sender.accessibilityIdentifier ?? "")) { (result) in
            
            
            let st =  String((sender.accessibilityIdentifier?.split(separator: "=").last)!)
            
            let localLocation: URL = DefaultDownloadDir.appendingPathComponent("\(st)1")
            
            // 文档路径
            
            let documentInteractionController = UIDocumentInteractionController(url: localLocation)
            documentInteractionController.presentOpenInMenu(from: .zero, in: self.view, animated: true)
            
            
            
//            switch result {
//            case .success:
//
//
//
//            case let .failure(error):
//                print(error)
//            }
        }
    }
    
    
    
    func documentInteractionControllerDidDismissOptionsMenu(_ controller: UIDocumentInteractionController) {
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        
        if self.btnFile.accessibilityIdentifier?.isEmpty == false {
            let st =  String((self.btnFile.accessibilityIdentifier?.split(separator: "=").last)!)
            
            let localLocation: URL = DefaultDownloadDir.appendingPathComponent("\(st)")
            
            try? FileManager.default.removeItem(at: localLocation)
        }
        
        
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
